package com.enniswu.weitter.moment;

import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;

import com.enniswu.weitter.R;
import com.enniswu.weitter.base.BaseFragment;
import com.enniswu.weitter.data.local.SPSupport;
import com.enniswu.weitter.moment.post.PostActivity;
import com.enniswu.weitter.moment.postlist.PostListFragment;
import com.enniswu.weitter.widget.adapter.FragmentPagerAdapter;

import java.util.Arrays;

import butterknife.BindView;
import butterknife.OnClick;

public class MomentFragment extends BaseFragment {

    public static final int REQUEST_POST = 100;
    @BindView(R.id.vp_moment)
    ViewPager vpMoment;
    @BindView(R.id.tl_moment)
    TabLayout tlMoment;

    @Override
    protected int setContentView() {
        return R.layout.fragment_moment;
    }

    @Override
    protected void onCreateView(View contentView) {
        FragmentPagerAdapter adapter = new FragmentPagerAdapter(getChildFragmentManager(),
                Arrays.asList(PostListFragment.newInstance(PostListFragment.ORIENTATION_PUBLIC),
                        PostListFragment.newInstance(PostListFragment.ORIENTATION_FOLLOWING)));
        adapter.setTitles(Arrays.asList(getResources().getStringArray(R.array.moment_titles)));
        vpMoment.setAdapter(adapter);
        tlMoment.setupWithViewPager(vpMoment);
    }

    @OnClick(R.id.fab_add)
    public void post() {
        if (TextUtils.isEmpty(SPSupport.getInstance().getSessionId())) {
            showAlertLogin();
            return;
        }
        Intent intent = new Intent(getContext(), PostActivity.class);
        intent.putExtra("type", tlMoment.getSelectedTabPosition());
        startActivityForResult(intent, REQUEST_POST);
    }
}
